/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.service;

import br.com.foxline.clinicplus.entity.Doctor;
import br.com.foxline.clinicplus.entity.Procedure;
import br.com.foxline.clinicplus.entity.WaitingLine;
import br.com.foxline.clinicplus.enums.DiarySituation;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author roniere
 */
@Stateless
public class WaitingLineService extends GenericService<WaitingLine> {

    public WaitingLineService() {
        super(WaitingLine.class);
    }

    public void setWaitingLine(Long id) {
        WaitingLine waitingLine = find(id);
        if (waitingLine != null) {
            waitingLine.setTimeAdmission(new Date());
            waitingLine.setStatus(false);
            update(waitingLine);
        }
    }

    public WaitingLine getWaitingLineForDiary(Long d) {
        String sql = "Select w from WaitingLine w JOIN w.diary d where d.id = :id";
        Query createQuery = getEntityManager().createQuery(sql);
        createQuery.setParameter("id", d);

        return createQuery.getResultList().isEmpty() ? null : (WaitingLine) createQuery.getResultList().get(0);
    }

    public List<WaitingLine> getWaitingLineForProcedure(Procedure procedure, Doctor doctor) {

        Calendar gc = GregorianCalendar.getInstance();
        gc.set(GregorianCalendar.HOUR_OF_DAY, 0);
        gc.set(GregorianCalendar.MINUTE, 0);
        gc.set(GregorianCalendar.SECOND, 0);
        gc.set(GregorianCalendar.MILLISECOND, 0);


        String sql = "Select w from WaitingLine w JOIN w.diary d";
        if (procedure != null && procedure.getId() != null) {
            sql += " JOIN d.procedure p ";
        }
        if (doctor != null && doctor.getId() != null) {
            sql += " JOIN d.doctor doc";
        }
        sql += " where 1 = 1";

        sql += " and d.startTime BETWEEN :datei AND :datef ";
                
        if (procedure != null && procedure.getId() != null) {
            sql += " and p.id = :procedure ";
        }
        if (doctor != null && doctor.getId() != null) {
            sql += " and doc.id = :doctor ";
        }
        sql += " and "
                + " d.diarySituation = :diarySituation and d.status = :status and"
                + " d.cancel = :cancel and w.status = :status1"
                + " order by w.id asc";
        Query createQuery = getEntityManager().createQuery(sql);
        if (procedure != null && procedure.getId() != null) {
            createQuery.setParameter("procedure", procedure.getId());
        }
        if (doctor != null && doctor.getId() != null) {
            createQuery.setParameter("doctor", doctor.getId());
        }
        createQuery.setParameter("diarySituation", DiarySituation.CALL_WAITING);
        createQuery.setParameter("status", true);
        createQuery.setParameter("cancel", false);
        createQuery.setParameter("status1", true);
        
        
        createQuery.setParameter("datei", gc.getTime());
        gc.add(GregorianCalendar.DAY_OF_MONTH, 1);
        createQuery.setParameter("datef", gc.getTime());

        return createQuery.getResultList();
    }

    public void getWaitingLineForProcedure(Long id) {
        String sql = "Select w from WaitingLine w JOIN w.diary d "
                + " where d.id = :id and w.status = :status";
        Query createQuery = getEntityManager().createQuery(sql);
        createQuery.setParameter("id", id);
        createQuery.setParameter("status", true);


        WaitingLine waitingLine = null;
        try {
            waitingLine = (WaitingLine) createQuery.getResultList().get(0);
        } catch (Exception e) {
            return;
        }

        if (waitingLine != null) {
            waitingLine.setTimeAdmission(new Date());
            waitingLine.setStatus(false);
            update(waitingLine);
        }

    }
}
